﻿<template>
  <a-modal
    title="编辑任务记录"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="DbTaskId" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入DbTaskId" v-decorator="['dbTaskId', {rules: [{required: true, message: '请输入DbTaskId！'}]}]" />
        </a-form-item>
        <a-form-item label="计划执行时间" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-date-picker style="width: 100%" placeholder="请选择计划执行时间" v-decorator="['planTime',{rules: [{ required: true, message: '请选择计划执行时间！' }]}]" @change="onChangeplanTime"/>
        </a-form-item>
        <a-form-item label="执行开始时间" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-date-picker style="width: 100%" placeholder="请选择执行开始时间" v-decorator="['startTime',{rules: [{ required: true, message: '请选择执行开始时间！' }]}]" @change="onChangestartTime"/>
        </a-form-item>
        <a-form-item label="执行结束时间" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-date-picker style="width: 100%" placeholder="请选择执行结束时间" v-decorator="['endTime',{rules: [{ required: true, message: '请选择执行结束时间！' }]}]" @change="onChangeendTime"/>
        </a-form-item>
        <a-form-item label="执行状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select style="width: 100%" placeholder="请选择执行状态" v-decorator="['status', {rules: [{ required: true, message: '请选择执行状态！' }]}]">
            <a-select-option v-for="(item,index) in statusData" :key="index" :value="item.code">{{ item.name }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="当前步骤" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input-number placeholder="请输入当前步骤" style="width: 100%" v-decorator="['currentStepNo']" />
        </a-form-item>
        <a-form-item label="任务错误消息" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入任务错误消息" v-decorator="['errorMessage']" />
        </a-form-item>
        <a-form-item label="执行的数据库ID" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入执行的数据库ID" v-decorator="['dbId']" />
        </a-form-item>
        <a-form-item label="任务ID" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入任务ID" v-decorator="['policyTaskId']" />
        </a-form-item>
        <a-form-item label="行标记" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入行标记" v-decorator="['rowflag']" />
        </a-form-item>
        <a-form-item label="执行临时信息" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入执行临时信息" v-decorator="['context']" />
        </a-form-item>
        <a-form-item v-show="false"><a-input v-decorator="['id']" /></a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import moment from 'moment'
  import {
    TaskRecordEdit
  } from '@/api/modular/main/TaskRecordManage'
  export default {
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        record: {},
        planTimeDateString: '',
        startTimeDateString: '',
        endTimeDateString: '',
        statusData: [],
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this)
      }
    },
    methods: {
	moment,
	// 初始化方法
	edit (record) {
	this.visible = true
	this.record = record
	const statusOption = this.$options
	this.statusData = statusOption.filters['enumData']('ExecuteStatus')
	setTimeout(() => {
	this.form.setFieldsValue(
	{
	id: record.id,
	dbTaskId: record.dbTaskId,
	status: record.status,
	currentStepNo: record.currentStepNo,
	errorMessage: record.errorMessage,
	dbId: record.dbId,
	policyTaskId: record.policyTaskId,
	rowflag: record.rowflag,
	context: record.context
	}
	)
	}, 100)
	this.form.getFieldDecorator('planTime', { initialValue: moment(record.planTime, 'YYYY-MM-DD') })
	this.planTimeDateString = moment(record.planTime).format('YYYY-MM-DD')
	this.form.getFieldDecorator('startTime', { initialValue: moment(record.startTime, 'YYYY-MM-DD') })
	this.startTimeDateString = moment(record.startTime).format('YYYY-MM-DD')
	this.form.getFieldDecorator('endTime', { initialValue: moment(record.endTime, 'YYYY-MM-DD') })
	this.endTimeDateString = moment(record.endTime).format('YYYY-MM-DD')
	},
	handleSubmit () {
	const { form: { validateFields } } = this
	this.confirmLoading = true
	validateFields((errors, values) => {
	if (!errors) {
	for (const key in values) {
	// WARN: 有可能导致类型错误，注意
	if ((values[key]) !== null && typeof (values[key]) === 'object') {
	values[key] = JSON.stringify(values[key])
	this.record[key] = values[key]
	} else {
	this.record[key] = values[key]
	}
	}
	values.planTime = this.planTimeDateString
	this.record.planTime = this.planTimeDateString
	values.startTime = this.startTimeDateString
	this.record.startTime = this.startTimeDateString
	values.endTime = this.endTimeDateString
	this.record.endTime = this.endTimeDateString
	TaskRecordEdit(this.record).then((res) => {
	if (res.success) {
	this.$message.success('编辑成功')
	this.confirmLoading = false
	this.$emit('ok', this.record)
	this.handleCancel()
	} else {
	this.$message.error('编辑失败：' + JSON.stringify(res.message))
	}
	}).finally((res) => {
	this.confirmLoading = false
	})
	} else {
	this.confirmLoading = false
	}
	})
	},
	onChangeplanTime(date, dateString) {
	this.planTimeDateString = dateString
	},
	onChangestartTime(date, dateString) {
	this.startTimeDateString = dateString
	},
	onChangeendTime(date, dateString) {
	this.endTimeDateString = dateString
	},
	handleCancel () {
	this.form.resetFields()
	this.visible = false
	}
	}
	}
</script>
